#!/usr/bin/python3

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json
import os

from distutils.version import LooseVersion
from keystoneauth1.identity import v3
from keystoneauth1 import session
{% if baremetal_provision|default(false)|bool %}
from metalsmith import Provisioner
{% endif %}
from novaclient import __version__ as nc_version
from novaclient import client
import openstack
from openstack import connection
try:
    from urllib.parse import urljoin
except ImportError:
    from urlparse import urljoin

cloud_name = os.environ.get('OS_CLOUD')

if cloud_name:
   conn = openstack.connect(cloud=cloud_name)
else:
    auth_url = os.environ["OS_AUTH_URL"]
    if os.environ.get("OS_IDENTITY_API_VERSION") == "3":
        if 'v3' not in auth_url:
            auth_url = urljoin(auth_url, 'v3')
    username = os.environ.get("OS_USERNAME")
    password = os.environ.get("OS_PASSWORD")
    project_name = os.environ.get("OS_TENANT_NAME",
                                  os.environ.get("OS_PROJECT_NAME"))
    user_domain_name = os.environ.get("OS_USER_DOMAIN_NAME")
    project_domain_name = os.environ.get("OS_PROJECT_DOMAIN_NAME")

    auth = v3.Password(auth_url=auth_url,
                       username=username,
                       password=password,
                       project_name=project_name,
                       user_domain_name=user_domain_name,
                       project_domain_name=project_domain_name,
                       )
    session = session.Session(auth=auth, verify=False)
    conn = connection.Connection(session=session)

{% if baremetal_provision|default(false)|bool %}
provisioner = Provisioner(cloud_region=conn.config)
provisioner.connection = conn
servers_list = provisioner.list_instances()
oc_servers = {server.hostname: server.ip_addresses()['ctlplane'][0]
              for server in servers_list
              if server.ip_addresses().get('ctlplane')}
{% else %}
nova = client.Client(2, session=session)
oc_servers = {server.name: server.networks['ctlplane'][0]
              for server in nova.servers.list()
              if server.networks.get('ctlplane')}
{% endif %}
print(json.dumps(oc_servers, indent=4))
